﻿namespace DotRas
{
    using pptpclient.Properties;
    using System;
    using System.Collections.ObjectModel;
    using System.Diagnostics;
    using System.Net;
    using System.Security.Permissions;

    [DebuggerDisplay("Name = {Name}, PhoneNumber = {PhoneNumber}"), PermissionSet(SecurityAction.Demand, Name="FullTrust")]
    public sealed class RasEntry : MarshalByRefObject, ICloneable
    {
        private Collection<string> _alternatePhoneNumbers;
        private string _areaCode;
        private string _autoDialDll;
        private string _autoDialFunc;
        private int _channels;
        private int _countryCode;
        private int _countryId;
        private int _customAuthKey;
        private string _customDialDll;
        private RasDevice _device;
        private int _dialExtraPercent;
        private int _dialExtraSampleSeconds;
        private RasDialMode _dialMode;
        private System.Net.IPAddress _dnsAddress;
        private System.Net.IPAddress _dnsAddressAlt;
        private RasEncryptionType _encryptionType;
        private RasEntryType _entryType;
        private int _frameSize;
        private RasFramingProtocol _framingProtocol;
        private int _hangUpExtraPercent;
        private int _hangUpExtraSampleSeconds;
        private Guid _id;
        private int _idleDisconnectSeconds;
        private System.Net.IPAddress _ipAddress;
        private string _name;
        private RasNetworkProtocols _networkProtocols;
        private RasEntryOptions _options;
        private RasPhoneBook _owner;
        private string _phoneNumber;
        private int _reserved1;
        private int _reserved2;
        private string _script;
        private RasSubEntryCollection _subEntries;
        private RasVpnStrategy _vpnStrategy;
        private System.Net.IPAddress _winsAddress;
        private System.Net.IPAddress _winsAddressAlt;
        private string _x25Address;
        private string _x25Facilities;
        private string _x25PadType;
        private string _x25UserData;

        public RasEntry(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                DotRas.ThrowHelper.ThrowArgumentException("name", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            this._name = name;
            this._x25Address = string.Empty;
            this._x25PadType = string.Empty;
        }

        public bool ClearCredentials()
        {
            if (this.Owner == null)
            {
                DotRas.ThrowHelper.ThrowInvalidOperationException(Resources.Exception_EntryNotInPhoneBook);
            }
            NativeMethods.RASCREDENTIALS credentials = new NativeMethods.RASCREDENTIALS {
                userName = string.Empty,
                password = string.Empty,
                domain = string.Empty,
                options = NativeMethods.RASCM.Domain | NativeMethods.RASCM.Password | NativeMethods.RASCM.UserName
            };
            return DotRas.RasHelper.SetCredentials(this.Owner.Path, this.Name, credentials, true);
        }

        public object Clone()
        {
            RasEntry entry = new RasEntry(this.Name);
            if ((this.AlternatePhoneNumbers != null) && (this.AlternatePhoneNumbers.Count > 0))
            {
                entry.AlternatePhoneNumbers = new Collection<string>();
                foreach (string str in this.AlternatePhoneNumbers)
                {
                    entry.AlternatePhoneNumbers.Add(str);
                }
            }
            entry.AreaCode = this.AreaCode;
            entry.AutoDialDll = this.AutoDialDll;
            entry.AutoDialFunc = this.AutoDialFunc;
            entry.Channels = this.Channels;
            entry.CountryCode = this.CountryCode;
            entry.CountryId = this.CountryId;
            entry.CustomAuthKey = this.CustomAuthKey;
            entry.CustomDialDll = this.CustomDialDll;
            entry.Device = this.Device;
            entry.DialExtraPercent = this.DialExtraPercent;
            entry.DialExtraSampleSeconds = this.DialExtraSampleSeconds;
            entry.DialMode = this.DialMode;
            entry.DnsAddress = this.DnsAddress;
            entry.DnsAddressAlt = this.DnsAddressAlt;
            entry.EncryptionType = this.EncryptionType;
            entry.EntryType = this.EntryType;
            entry.FrameSize = this.FrameSize;
            entry.FramingProtocol = this.FramingProtocol;
            entry.HangUpExtraPercent = this.HangUpExtraPercent;
            entry.HangUpExtraSampleSeconds = this.HangUpExtraSampleSeconds;
            entry.IdleDisconnectSeconds = this.IdleDisconnectSeconds;
            entry.IPAddress = this.IPAddress;
            entry.NetworkProtocols = this.NetworkProtocols;
            entry.Options = this.Options;
            entry.PhoneNumber = this.PhoneNumber;
            entry.Reserved1 = this.Reserved1;
            entry.Reserved2 = this.Reserved2;
            entry.Script = this.Script;
            if ((this.SubEntries != null) && (this.SubEntries.Count > 0))
            {
                foreach (RasSubEntry entry2 in this.SubEntries)
                {
                    entry.SubEntries.Add((RasSubEntry) entry2.Clone());
                }
            }
            entry.VpnStrategy = this.VpnStrategy;
            entry.WinsAddress = this.WinsAddress;
            entry.WinsAddressAlt = this.WinsAddressAlt;
            entry.X25Address = this.X25Address;
            entry.X25Facilities = this.X25Facilities;
            entry.X25PadType = this.X25PadType;
            entry.X25UserData = this.X25UserData;
            return entry;
        }

        public static RasEntry CreateDialUpEntry(string name, string phoneNumber, RasDevice device)
        {
            if (string.IsNullOrEmpty(name))
            {
                DotRas.ThrowHelper.ThrowArgumentException("name", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            if (string.IsNullOrEmpty(phoneNumber))
            {
                DotRas.ThrowHelper.ThrowArgumentException("phoneNumber", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            if (device == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("device");
            }
            return new RasEntry(name) { Device = device, DialMode = RasDialMode.None, EntryType = RasEntryType.Phone, FramingProtocol = RasFramingProtocol.Ppp, IdleDisconnectSeconds = 0, NetworkProtocols = RasNetworkProtocols.IP, Options = RasEntryOptions.None, PhoneNumber = phoneNumber, VpnStrategy = RasVpnStrategy.Default };
        }

        public static RasEntry CreateVpnEntry(string name, string serverAddress, RasVpnStrategy strategy, RasDevice device)
        {
            if (string.IsNullOrEmpty(name))
            {
                DotRas.ThrowHelper.ThrowArgumentException("name", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            if (string.IsNullOrEmpty(serverAddress))
            {
                DotRas.ThrowHelper.ThrowArgumentException("serverAddress", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            if (device == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("device");
            }
            return new RasEntry(name) { Device = device, EncryptionType = RasEncryptionType.Require, EntryType = RasEntryType.Vpn, FramingProtocol = RasFramingProtocol.Ppp, NetworkProtocols = RasNetworkProtocols.IP, Options = RasEntryOptions.PreviewDomain | RasEntryOptions.PreviewUserPassword | RasEntryOptions.ShowDialingProgress | RasEntryOptions.RemoteDefaultGateway | RasEntryOptions.RequireEncryptedPassword | RasEntryOptions.ModemLights, PhoneNumber = serverAddress, VpnStrategy = strategy };
        }

        public NetworkCredential GetCredentials()
        {
            if (this.Owner == null)
            {
                DotRas.ThrowHelper.ThrowInvalidOperationException(Resources.Exception_EntryNotInPhoneBook);
            }
            return DotRas.RasHelper.GetCredentials(this.Owner.Path, this.Name, NativeMethods.RASCM.Domain | NativeMethods.RASCM.Password | NativeMethods.RASCM.UserName);
        }

        private bool InternalSetCredentials(NetworkCredential value, bool storeCredentialsForAllUsers)
        {
            NativeMethods.RASCREDENTIALS credentials = new NativeMethods.RASCREDENTIALS {
                userName = value.UserName,
                password = value.Password,
                domain = value.Domain,
                options = NativeMethods.RASCM.Domain | NativeMethods.RASCM.Password | NativeMethods.RASCM.UserName
            };
            return DotRas.RasHelper.SetCredentials(this.Owner.Path, this.Name, credentials, false);
        }

        public bool Remove()
        {
            bool flag = false;
            if (this.Owner != null)
            {
                flag = this.Owner.Entries.Remove(this);
            }
            return flag;
        }

        public bool Rename(string newEntryName)
        {
            if (string.IsNullOrEmpty(newEntryName))
            {
                DotRas.ThrowHelper.ThrowArgumentException("newEntryName", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            bool flag = false;
            if (this.Owner == null)
            {
                this.Name = newEntryName;
                return true;
            }
            if (!DotRas.RasHelper.IsValidEntryName(this.Owner, newEntryName))
            {
                DotRas.ThrowHelper.ThrowArgumentException("newEntryName", Resources.Argument_InvalidEntryName, new object[] { newEntryName });
            }
            if (DotRas.RasHelper.RenameEntry(this.Owner, this.Name, newEntryName))
            {
                this.Owner.Entries.ChangeKey(this, newEntryName);
                this.Name = newEntryName;
                flag = true;
            }
            return flag;
        }

        public bool Update()
        {
            if (this.Owner == null)
            {
                DotRas.ThrowHelper.ThrowInvalidOperationException(Resources.Exception_EntryNotInPhoneBook);
            }
            return DotRas.RasHelper.SetEntryProperties(this.Owner, this);
        }

        public bool UpdateCredentials(NetworkCredential value)
        {
            if (this.Owner == null)
            {
                DotRas.ThrowHelper.ThrowInvalidOperationException(Resources.Exception_EntryNotInPhoneBook);
            }
            if (value == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("value");
            }
            return this.InternalSetCredentials(value, false);
        }

        public Collection<string> AlternatePhoneNumbers
        {
            get
            {
                if (this._alternatePhoneNumbers == null)
                {
                    this._alternatePhoneNumbers = new Collection<string>();
                }
                return this._alternatePhoneNumbers;
            }
            internal set
            {
                this._alternatePhoneNumbers = value;
            }
        }

        public string AreaCode
        {
            get
            {
                return this._areaCode;
            }
            set
            {
                this._areaCode = value;
            }
        }

        [Obsolete("This member is no longer used. The CustomDialDll property should be used instead.")]
        public string AutoDialDll
        {
            get
            {
                return this._autoDialDll;
            }
            set
            {
                this._autoDialDll = value;
            }
        }

        [Obsolete("This member is no longer used.")]
        public string AutoDialFunc
        {
            get
            {
                return this._autoDialFunc;
            }
            set
            {
                this._autoDialFunc = value;
            }
        }

        public int Channels
        {
            get
            {
                return this._channels;
            }
            set
            {
                this._channels = value;
            }
        }

        public int CountryCode
        {
            get
            {
                return this._countryCode;
            }
            set
            {
                this._countryCode = value;
            }
        }

        public int CountryId
        {
            get
            {
                return this._countryId;
            }
            set
            {
                this._countryId = value;
            }
        }

        public int CustomAuthKey
        {
            get
            {
                return this._customAuthKey;
            }
            set
            {
                this._customAuthKey = value;
            }
        }

        public string CustomDialDll
        {
            get
            {
                return this._customDialDll;
            }
            set
            {
                this._customDialDll = value;
            }
        }

        public RasDevice Device
        {
            get
            {
                return this._device;
            }
            set
            {
                this._device = value;
            }
        }

        public int DialExtraPercent
        {
            get
            {
                return this._dialExtraPercent;
            }
            set
            {
                this._dialExtraPercent = value;
            }
        }

        public int DialExtraSampleSeconds
        {
            get
            {
                return this._dialExtraSampleSeconds;
            }
            set
            {
                this._dialExtraSampleSeconds = value;
            }
        }

        public RasDialMode DialMode
        {
            get
            {
                return this._dialMode;
            }
            set
            {
                this._dialMode = value;
            }
        }

        public System.Net.IPAddress DnsAddress
        {
            get
            {
                return this._dnsAddress;
            }
            set
            {
                this._dnsAddress = value;
            }
        }

        public System.Net.IPAddress DnsAddressAlt
        {
            get
            {
                return this._dnsAddressAlt;
            }
            set
            {
                this._dnsAddressAlt = value;
            }
        }

        public RasEncryptionType EncryptionType
        {
            get
            {
                return this._encryptionType;
            }
            set
            {
                this._encryptionType = value;
            }
        }

        public RasEntryType EntryType
        {
            get
            {
                return this._entryType;
            }
            set
            {
                this._entryType = value;
            }
        }

        public int FrameSize
        {
            get
            {
                return this._frameSize;
            }
            set
            {
                this._frameSize = value;
            }
        }

        public RasFramingProtocol FramingProtocol
        {
            get
            {
                return this._framingProtocol;
            }
            set
            {
                this._framingProtocol = value;
            }
        }

        public int HangUpExtraPercent
        {
            get
            {
                return this._hangUpExtraPercent;
            }
            set
            {
                this._hangUpExtraPercent = value;
            }
        }

        public int HangUpExtraSampleSeconds
        {
            get
            {
                return this._hangUpExtraSampleSeconds;
            }
            set
            {
                this._hangUpExtraSampleSeconds = value;
            }
        }

        public Guid Id
        {
            get
            {
                return this._id;
            }
            internal set
            {
                this._id = value;
            }
        }

        public int IdleDisconnectSeconds
        {
            get
            {
                return this._idleDisconnectSeconds;
            }
            set
            {
                this._idleDisconnectSeconds = value;
            }
        }

        public System.Net.IPAddress IPAddress
        {
            get
            {
                return this._ipAddress;
            }
            set
            {
                this._ipAddress = value;
            }
        }

        public string Name
        {
            get
            {
                return this._name;
            }
            private set
            {
                this._name = value;
            }
        }

        public RasNetworkProtocols NetworkProtocols
        {
            get
            {
                return this._networkProtocols;
            }
            set
            {
                this._networkProtocols = value;
            }
        }

        public RasEntryOptions Options
        {
            get
            {
                return this._options;
            }
            set
            {
                this._options = value;
            }
        }

        public RasPhoneBook Owner
        {
            get
            {
                return this._owner;
            }
            internal set
            {
                this._owner = value;
            }
        }

        public string PhoneNumber
        {
            get
            {
                return this._phoneNumber;
            }
            set
            {
                this._phoneNumber = value;
            }
        }

        public int Reserved1
        {
            get
            {
                return this._reserved1;
            }
            set
            {
                this._reserved1 = value;
            }
        }

        public int Reserved2
        {
            get
            {
                return this._reserved2;
            }
            set
            {
                this._reserved2 = value;
            }
        }

        public string Script
        {
            get
            {
                return this._script;
            }
            set
            {
                this._script = value;
            }
        }

        public RasSubEntryCollection SubEntries
        {
            get
            {
                if (this._subEntries == null)
                {
                    this._subEntries = new RasSubEntryCollection(this);
                }
                return this._subEntries;
            }
        }

        public RasVpnStrategy VpnStrategy
        {
            get
            {
                return this._vpnStrategy;
            }
            set
            {
                this._vpnStrategy = value;
            }
        }

        public System.Net.IPAddress WinsAddress
        {
            get
            {
                return this._winsAddress;
            }
            set
            {
                this._winsAddress = value;
            }
        }

        public System.Net.IPAddress WinsAddressAlt
        {
            get
            {
                return this._winsAddressAlt;
            }
            set
            {
                this._winsAddressAlt = value;
            }
        }

        public string X25Address
        {
            get
            {
                return this._x25Address;
            }
            set
            {
                this._x25Address = value;
            }
        }

        public string X25Facilities
        {
            get
            {
                return this._x25Facilities;
            }
            set
            {
                this._x25Facilities = value;
            }
        }

        public string X25PadType
        {
            get
            {
                return this._x25PadType;
            }
            set
            {
                this._x25PadType = value;
            }
        }

        public string X25UserData
        {
            get
            {
                return this._x25UserData;
            }
            set
            {
                this._x25UserData = value;
            }
        }
    }
}

